package com.android.phoenix.activity;

import com.android.phoenix.Constant;
import com.android.phoenix.MainApplication;
import com.android.phoenix.R;

import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;


public class ChangePassActivity extends BaseActivity{
	
	private Button btnConfirm;
	private ImageView imgBack;
	private EditText etOldPass;
	private EditText etNewPass;
	private EditText etConfirmPass;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acti_change_pass);
		
		etOldPass = (EditText) findViewById(R.id.etOldPass);
		etNewPass = (EditText) findViewById(R.id.etNewPass);
		etConfirmPass = (EditText) findViewById(R.id.etConfirmPass);
		
		btnConfirm = (Button) findViewById(R.id.btnConfirm);
		btnConfirm.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String oldPassword = MainApplication.mSharedPreferences.getString(Constant.PASSWORD, "");
				if(etOldPass.getText().toString().trim().equals(oldPassword)){
					if(etNewPass.getText().toString().trim().equals(etConfirmPass.getText().toString().trim())){
						Intent intent = new Intent();
						intent.setClass(ChangePassActivity.this, SettingsActivity.class);
						startActivity(intent);
						savePassword();
						Toast.makeText(ChangePassActivity.this, getResources().getString(R.string.txt_change_password_success), 
								Toast.LENGTH_SHORT).show();
					} else {
						etNewPass.setText("");
						etConfirmPass.setText("");
						Toast.makeText(ChangePassActivity.this, getResources().getString(R.string.txt_password_error), 
								Toast.LENGTH_SHORT).show();
					}
				} else {
					etOldPass.setText("");
					etNewPass.setText("");
					etConfirmPass.setText("");
					Toast.makeText(ChangePassActivity.this, getResources().getString(R.string.txt_old_password_error), 
							Toast.LENGTH_SHORT).show();
				}
				
				
			}
		});
		
		imgBack = (ImageView) findViewById(R.id.imgBack);
		
		imgBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	private void savePassword(){
		Editor edit = MainApplication.mSharedPreferences.edit();
		edit.remove(Constant.PASSWORD);
		edit.putString(Constant.PASSWORD, etConfirmPass.getText().toString().trim());
		edit.commit();
	}

}
